package com.zyd.blog.personal.controller;

import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.personal.enuminterface.Status;
import com.zyd.blog.personal.service.UserProductionService;
import com.zyd.blog.personal.service.UserService;
import com.zyd.blog.personal.utils.RespResultUtils;
import com.zyd.blog.personal.vo.ResponseResultVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 用户基本信息Controller
 * @author mfy
 */
@RestController
@RequestMapping("/personal")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private UserProductionService userProductionService;

    /**
     * 用户主页信息展示 1
     *  访问路径  http://localhost:8443/personal/home?userId=1&selfId=
     * @param userId 用户id
     * @param selfId    自己id
     * @return  用户基本信息
     */
    @RequestMapping("/home")
    public Object home(Long userId,Long selfId){
        ResponseResultVo resultVo;
        if(userId == null){
            //  用户id为空
            resultVo = RespResultUtils.getFailResult("用户id不能为空");
        }else{
            resultVo = RespResultUtils.getSuccessData(userService.getUserInfo(userId,selfId));
        }
        return resultVo;
    }

    /**
     * 查询用户关注信息 2
     * 访问路径 http://localhost:8443/personal/getFocusInfo?userId=1&focusId=
     * @param userId    用户id
     * @param focusId   关注用户id
     * @return  全部关注信息
     */
    @RequestMapping("/getFocusInfo")
    public Object getFocusInfo(Long userId,Long focusId){
        ResponseResultVo resultVo;
        if(userId == null){
            //  用户id为空
            resultVo = RespResultUtils.getFailResult("用户id不能为空");
        }else{
            resultVo = RespResultUtils.getSuccessData(userService.getFocusInfo(userId,focusId));
        }
        return resultVo;
    }

    /**
     * 查询用户粉丝信息 3
     * 访问路径 http://localhost:8443/personal/getFansInfo?userId=4
     * @param userId    用户id
     * @return  粉丝信息
     */
    @RequestMapping("/getFansInfo")
    public Object getFansInfo(Long userId){
        ResponseResultVo resultVo ;
        if(userId == null){
            //  用户id为空
            resultVo = RespResultUtils.getFailResult("用户id不能为空");
        }else{
            resultVo = RespResultUtils.getSuccessData(userService.getFansInfo(userId));
        }
        return resultVo;
    }

    /**
     * 得到用户作品列表 4
     * 访问路径  http://localhost:8443/personal/getUserProduction?userId=1&selfId=
     * @param userId    用户id
     * @param selfId    自己id
     * @return  作品列表
     */
    @RequestMapping("/getUserProduction")
    public Object getUserProduction(Long userId,Long selfId){
        ResponseResultVo resultVo;
        if(userId == null){
            //  用户id为空
            resultVo = RespResultUtils.getFailResult("用户id不能为空");
        }else{
            resultVo = RespResultUtils.getSuccessData(userProductionService.getUserProduction(selfId,null,userId, Status.STATUS_YES, IsDelete.IS_DELETE_NO));
        }
        return resultVo;
    }

    /**
     * 关注其他用户 5
     * 访问路径 http://localhost:8443/personal/focusUser?userId=3&focusUserId=1
     * @param userId    用户id
     * @param focusUserId   关注对象id
     * @return  操作结果
     */
    @RequestMapping("/focusUser")
    public Object focusUser(Long userId,Long focusUserId){
        ResponseResultVo resultVo;
        if(userId == null && focusUserId == null){
            resultVo = RespResultUtils.getFailResult("用户id或者关注用户id不能为空");
        }else{
            Integer flag = (Integer)userService.focusUser(userId,focusUserId);
            switch (flag){
                case -1:
                    resultVo = RespResultUtils.getFailResult("关注失败");
                    break;
                case 0:
                    resultVo = RespResultUtils.getSuccessBoolean("关注成功");
                    break;
                case 1:
                    resultVo = RespResultUtils.getFailResult("用户不存在或者已被冻结");
                    break;
                case 2:
                    resultVo = RespResultUtils.getFailResult("所关注用户不存在或者已被冻结");
                    break;
                case 3:
                    resultVo = RespResultUtils.getFailResult("已经关注该用户");
                    break;
                default:
                    resultVo = new ResponseResultVo();
            }
        }
        return resultVo;
    }

    /**
     * 取消关注 6
     * 访问路径 http://localhost:8443/personal/noFocus?userId=3&focusUserId=1
     * @param userId    用户id
     * @param focusUserId   关注对象id
     * @return  操作结果
     */
    @RequestMapping("/noFocus")
    public Object noFocus(Long userId,Long focusUserId){
        ResponseResultVo resultVo;
        if(userId == null && focusUserId == null){
            resultVo = RespResultUtils.getFailResult("用户id或者所关注用户id不能为空");
        }else{
            int flag = (int)userService.noFocus(userId,focusUserId);
            switch (flag){
                case -1:
                    resultVo = RespResultUtils.getFailResult("取消关注失败");
                    break;
                case 0:
                    resultVo = RespResultUtils.getSuccessBoolean("取消关注成功");
                    break;
                case 1:
                    resultVo = RespResultUtils.getFailResult("用户不存在或者已被冻结");
                    break;
                case 2:
                    resultVo = RespResultUtils.getFailResult("并未关注该用户");
                    break;
                default:
                    resultVo = new ResponseResultVo();
            }
        }
        return resultVo;
    }


    /**
     * 收藏作品 7
     * 访问路径 http://localhost:8443/personal/collectionProduction?userId=3&productionId=1
     * @param userId    用户id
     * @param productionId  作品id
     * @return  操作结果
     */
    @RequestMapping("/collectionProduction")
    public Object collectionProduction(Long userId,Long productionId){
        ResponseResultVo resultVo;
        if(userId == null && productionId == null){
            resultVo = RespResultUtils.getFailResult("用户id或者作品id不存在");
        }else{
            if((Boolean) userService.collectionProduction(userId,productionId)){
                resultVo = RespResultUtils.getSuccessBoolean("收藏成功");
            }else{
                resultVo = RespResultUtils.getFailResult("收藏失败");
            }
        }
        return resultVo;
    }

    /**
     * 取消收藏 8
     * 访问路径 http://localhost:8443/personal/noCollectionProduction?userId=3&productionId=1
     * @param userId    用户id
     * @param productionId  作品id
     * @return  操作结果
     */
    @RequestMapping("/noCollectionProduction")
    public Object noCollectionProduction(Long userId,Long productionId){
        ResponseResultVo resultVo ;
        if(userId == null && productionId == null){
            resultVo = RespResultUtils.getFailResult("用户id或者作品id不能为空");
        }else{
            if((Boolean)userService.noCollectionProduction(userId,productionId)){
                resultVo = RespResultUtils.getSuccessBoolean("成功删除收藏");
            }else{
                resultVo = RespResultUtils.getFailResult("取消收藏失败");
            }
        }
        return resultVo;
    }

    /**
     * 得到作品点赞数量 9
     * 访问路径 http://localhost:8443/personal/getThumbNum?productionId=1
     * @param productionId  作品id
     * @return  作品点赞数
     */
    @RequestMapping("/getThumbNum")
    public Object getThumbNum(Long productionId){
        ResponseResultVo resultVo ;
        if(productionId == null){
            resultVo = RespResultUtils.getFailResult("作品id不能为空");
        }else{
            resultVo = RespResultUtils.getSuccessData(userService.getThumbNum(productionId));
        }
        return resultVo;
    }

    /**
     * 点赞作品 10
     * 访问路径 http://localhost:8443/personal/thumb?userId=3&productionId=1
     * @param userId    用户id
     * @param productionId  作品id
     * @return  操作结果
     */
    @RequestMapping("/thumb")
    public Object thumb(Long userId,Long productionId){
        ResponseResultVo resultVo ;
        if(userId == null && productionId == null){
            resultVo = RespResultUtils.getFailResult("用户id或者作品id不能为空");
        }else{
            if((Boolean)userService.thumb(userId,productionId)){
                resultVo = RespResultUtils.getSuccessBoolean("点赞成功");
            }else{
                resultVo = RespResultUtils.getFailResult("点赞失败");
            }
        }
        return resultVo;
    }

    /**
     * 取消点赞 11
     * 访问路径 http://localhost:8443/personal/noThumb?userId=3&productionId=1
     * @param userId    用户id
     * @param productionId  作品id
     * @return  操作结果
     */
    @RequestMapping("/noThumb")
    public Object noThumb(Long userId,Long productionId){
        ResponseResultVo resultVo ;
        if(userId == null && productionId == null){
            resultVo = RespResultUtils.getFailResult("用户id或者作品id不能为空");
        }else{
            if((Boolean)userService.noThumb(userId,productionId)){
                resultVo = RespResultUtils.getSuccessBoolean("成功取消点赞");
            }else{
                resultVo = RespResultUtils.getFailResult("取消失败");
            }
        }
        return resultVo;
    }

    /**
     * 获得用户分享 12
     * 访问路径 http://localhost:8443/personal/getShare?userId=3&productionId=
     * @param userId    用户id
     * @return  分享列表
     */
    @RequestMapping("/getShare")
    public Object getShare(Long userId,Long productionId){
        ResponseResultVo resultVo;
        if(userId == null){
            resultVo = RespResultUtils.getFailResult("用户id不能为空");
        }else{
            resultVo = RespResultUtils.getSuccessData(userService.getShare(userId,productionId));
        }
        return resultVo;
    }

    /**
     * 分享别人作品 13
     * 访问路径 http://localhost:8443/personal/share?userId=3&productionId=1
     * @param userId    用户id
     * @param productionId  作品id
     * @return  操作结果
     */
    @RequestMapping("/share")
    public Object share(Long userId,Long productionId){
        ResponseResultVo resultVo ;
        if(userId == null && productionId == null){
            resultVo = RespResultUtils.getFailResult("用户id或者作品id不能为空");
        }else{
            if((Boolean)userService.share(userId,productionId)){
                resultVo = RespResultUtils.getSuccessBoolean("分享成功");
            }else{
                resultVo = RespResultUtils.getFailResult("分享失败");
            }
        }
        return resultVo;
    }

    /**
     * 删除分享 14
     * 访问路径 http://localhost:8443/personal/noShare?shareId=3
     * @param shareId   分享id
     * @return  操作结果
     */
    @RequestMapping("/noShare")
    public Object noShare(Long shareId){
        ResponseResultVo resultVo ;
        if(shareId == null){
            resultVo = RespResultUtils.getFailResult("id不能为空");
        }else{
            if((Boolean)userService.noShare(shareId)){
                resultVo = RespResultUtils.getSuccessBoolean("删除成功");
            }else{
                resultVo = RespResultUtils.getFailResult("删除失败");
            }
        }
        return resultVo;
    }

    /**
     * 查询作品的评论 15
     * 访问路径 http://localhost:8443/personal/getComment?productionId=3
     * @param productionId  作品id
     * @return  评论列表
     */
    @RequestMapping("/getComment")
    public Object getComment(Long productionId){
        ResponseResultVo resultVo;
        if(productionId == null){
            resultVo = RespResultUtils.getFailResult("作品id不能为空");
        }else {
            resultVo = RespResultUtils.getSuccessData(userService.getComment(productionId));
        }
        return resultVo;
    }

    /**
     * 添加评论 16
     * 访问路径 http://localhost:8443/personal/comment?userId=3&productionId=&commentId=&content=&ch=
     * @param userId    用户id
     * @param productionId  作品id
     * @param commentId 评论id
     * @param content   评论内容
     * @param ch    评论方式(1评论作品,2评论评论)
     * @return  操作结果
     */
    @RequestMapping("/comment")
    public Object comment(Long userId,Long productionId,Long commentId,String content,String ch){
        ResponseResultVo resultVo;
        if(userId == null && content== null && (ch == null || "".equals(ch))){
            resultVo = RespResultUtils.getFailResult("条件不能为空");
        }else{
            if(productionId == null || commentId == null){
                resultVo = RespResultUtils.getFailResult("条件不能为空");
            }else{
                if((Boolean)userService.comment(userId,productionId,commentId,content,ch)){
                    resultVo = RespResultUtils.getSuccessBoolean("评论成功");
                }else{
                    resultVo = RespResultUtils.getFailResult("评论失败");
                }
            }
        }
        return resultVo;
    }

    /**
     * 删除作品评论 17
     * 访问路径 http://localhost:8443/personal/deleteCommentPr?commentPrId=3
     * @param userId   用户id
     * @param commentPrId   作品评论id
     * @return  操作结果
     */
    @RequestMapping("/deleteCommentPr")
    public Object deleteCommentPr(Long userId,Long commentPrId){
        ResponseResultVo resultVo ;
        if(commentPrId == null){
            resultVo = RespResultUtils.getFailResult("评论id不能为空");
        }else{
            if((Boolean)userService.deleteCommentPr(userId,commentPrId)){
                resultVo = RespResultUtils.getSuccessBoolean("删除成功");
            }else{
                resultVo = RespResultUtils.getFailResult("删除失败");
            }
        }
        return resultVo;
    }

    /**
     * 删除评论评论 18
     * 访问路径 http://localhost:8443/personal/deleteCommentCo?commentCoId=3
     * @param commentCoId   评论id
     * @return  操作结果
     */
    @RequestMapping("/deleteCommentCo")
    public Object deleteCommentCo(Long userId,Long commentCoId){
        ResponseResultVo resultVo;
        if(commentCoId == null){
            resultVo = RespResultUtils.getFailResult("评论id不能为空");
        }else{
            if((Boolean)userService.deleteCommentCo(userId,commentCoId)){
                resultVo = RespResultUtils.getSuccessBoolean("删除成功");
            }else{
                resultVo = RespResultUtils.getFailResult("删除失败");
            }
        }
        return resultVo;
    }

    /**
     * 查询详细资料 19
     * 访问路径 http://localhost:8443/personal/getDesignerInfo?userId=3
     * @param userId    用户id
     * @return  返回详细信息
     */
    @RequestMapping("/getDesignerInfo")
    public Object getDesignerInfo(Long userId){
        ResponseResultVo resultVo ;
        if(userId == null){
            resultVo = RespResultUtils.getFailResult("用户id不能为空");
        }else{
            resultVo = RespResultUtils.getSuccessData(userService.getDesignerInfo(userId));
        }
        return resultVo;
    }

    /**
     * 得到otherId的关注列表,并展示userId是否有关注
     * 访问路径  http://localhost:8443/personal/getFacousOfFacous?userId=3&otherId=1
     * @param userId    用户id
     * @param otherId   查询用户id
     * @return  查询用户的关注列表
     */
    @RequestMapping("/getFacousOfFacous")
    public Object getFacousOfFacous(Long userId,Long otherId){
        ResponseResultVo resultVo ;
        if(otherId == null){
            resultVo = RespResultUtils.getFailResult("条件不能为空");
        }else{
            resultVo = RespResultUtils.getSuccessData(userService.getFacousOfFacous(userId,otherId));
        }
        return resultVo;
    }

    /**
     * 得到otherId的粉丝列表,并展示userId是否有关注
     * 访问路径 http://localhost:8443/personal/getFacusOfFan?userId=2&otherId=4
     * @param userId    用户id
     * @param otherId   查询用户id
     * @return  查询用户的粉丝列表
     */
    @RequestMapping("/getFacusOfFan")
    public Object getFacusOfFan(Long userId,Long otherId){
        ResponseResultVo resultVo ;
        if(otherId == null){
            resultVo = RespResultUtils.getFailResult("条件不能为空");
        }else{
            resultVo = RespResultUtils.getSuccessData(userService.getFacusOfFan(userId,otherId));
        }
        return resultVo;
    }

}
